//
//  RankingList.m
//  FB_skd
//
//  Created by Le Thanh Hai on 4/21/13.
//  Copyright (c) 2013 Le Thanh Hai. All rights reserved.
//

#import "RankingList.h"

@implementation RankingList
@synthesize dataSource;

- (id)init
{
    if (self = [super init]) {
        // Initialization code
        self = [[[NSBundle mainBundle] loadNibNamed:@"RankingList" owner:self options:nil] lastObject];
        
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"RankingList" owner:self options:nil] objectAtIndex:0];
        [view setFrame:[self bounds]];
        [self addSubview:view];
    }
    return self;
}

#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataSource count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 32.5f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static UINib *cellLoader;
    if (!cellLoader) {
        cellLoader = [[UINib nibWithNibName:@"CellRanking" bundle:[NSBundle mainBundle]] retain];
    }
    CellRanking *cell = (CellRanking*)[tableView dequeueReusableCellWithIdentifier:@"CellRanking"];
    if (cell == nil) {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }
    
    cell.name.text = [[[dataSource objectAtIndex:indexPath.row] objectForKey:@"user"] objectForKey:@"name"];
    cell.score.text = [NSString stringWithFormat:@"%@", [[dataSource objectAtIndex:indexPath.row] objectForKey:@"score"]];
    cell.number.text = [NSString stringWithFormat:@"%d", [indexPath row] + 1];
    cell.facebookId = [NSString stringWithFormat:@"%@", [[[dataSource objectAtIndex:indexPath.row] objectForKey:@"user"] objectForKey:@"id"]];
    return cell;
}

-(void)setDataSource:(NSMutableArray *)dataSource_
{
    dataSource = dataSource_;
    [tableRanking reloadData];
}

@end
